package com.heima.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.service.SensitiveService;
import com.heima.apis.wemedia.IWmSensitiveClient;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/29
 */
@Service
@Slf4j
public class SensitiveServiceImpl implements SensitiveService {
    @Autowired
    private IWmSensitiveClient wemediaClient;
    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(SensitiveDto dto) {
        if (ObjectUtil.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Page page = wemediaClient.getList(dto);
        ResponseResult result = new PageResponseResult(
                dto.getPage(), dto.getSize(), (int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 删除敏感词
     * @param id
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wemediaClient.delSensitive(id);
    }

    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive adSensitive) {
        if (ObjectUtil.isEmpty(adSensitive)
                || StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wemediaClient.saveSensitive(adSensitive);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive adSensitive) {
        if (ObjectUtil.isEmpty(adSensitive)
                || StringUtils.isBlank(adSensitive.getSensitives())
                || ObjectUtil.isEmpty(adSensitive.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wemediaClient.updateSensitive(adSensitive);
    }
}
